package com.hopool.shop.service.order;

import java.util.List;

import org.hibernate.Session;

import com.hopool.shop.exception.OrderException;
import com.hopool.shop.exception.ServiceException;
import com.hopool.shop.order.Order;
import com.hopool.shop.order.OrderProducts;
import com.hopool.shop.service.BaseService;

public class OrderService extends BaseService implements OrderServiceI{
	@Override
	public List page(Object exampleobj, Integer firstResult,
			Integer maxResults, String orderType, String... customProperties)
			throws ServiceException {
		return super.page(exampleobj, firstResult, maxResults, orderType,
				customProperties);
	}

	@Override
	public Integer count(Object exampleobj, String... customProperties)
			throws ServiceException {
		return super.count(exampleobj, customProperties);
	}

	public void addOrder(Order order)throws OrderException{
		getSession().save(order);
	}
	
	public void processOrder(Order order)throws OrderException{
		getSession().update(order);
	}
	
	public Order getOrderById(Long orderId)throws OrderException{
		return (Order)getSession().get(Order.class, orderId);
	}
	
	public Order getOrderByCode(String code)throws OrderException{
		return (Order) getSession()
				.createQuery("from Order o where o.orderNumber=?")
				.setString(0, code).uniqueResult();
	}
	
	public List<OrderProducts> getOrderDetail(Long orderId)throws OrderException{
		return getSession()
				.createQuery("from OrderProducts op where op.order.id=?")
				.setLong(0, orderId).list();
	}
	
	public void deleteOrder(Order order)throws OrderException{
		getSession().delete(order);
	}
	
	public OrderProducts getOrderProduct(Long opId)throws OrderException{
		return (OrderProducts) getSession().get(OrderProducts.class, opId);
	}
	
	public void modifyOrderProducts(List<OrderProducts> ops)throws OrderException{
		for(OrderProducts op:ops){
			getSession().saveOrUpdate(op);
		}
	}
	
	public void deleteOrderProducts(List<OrderProducts> ops)throws OrderException{
		for(OrderProducts op:ops){
			getSession().delete(op);
		}
	}
	
}
